﻿Imports MapWindow.Interfaces
Imports MapWinGIS
Imports System.Data

Public Class Analyze

    Public Sub CompareSkiPaths(ByVal mapWin As IMapWin)

        'Create the empty result table
        Dim resultTable As New DataTable
        resultTable.Columns.Add(New DataColumn("path"))
        resultTable.Columns.Add(New DataColumn("Normal Risk"))
        resultTable.Columns.Add(New DataColumn("Moderate Risk"))
        resultTable.Columns.Add(New DataColumn("High Risk"))
        resultTable.Columns.Add(New DataColumn("Very High Risk"))
        resultTable.Columns.Add(New DataColumn("Overall Risk"))

        For Each lay As Layer In mapWin.Layers

            If lay.LayerType = eLayerType.LineShapefile Then

                Dim pathShapefile As Shapefile = CType(lay.GetObject(), Shapefile)
                Dim resultRow As DataRow = resultTable.NewRow()
                resultRow(0) = pathShapefile.CellValue(0, 0) 'path name
                resultRow(1) = CType(pathShapefile.CellValue(1, 0), Integer) 'normal risk
                resultRow(2) = CType(pathShapefile.CellValue(2, 0), Integer) 'moderate risk
                resultRow(3) = CType(pathShapefile.CellValue(3, 0), Integer) 'high risk
                resultRow(4) = CType(pathShapefile.CellValue(4, 0), Integer) 'very high risk

                Dim overallRisk As Integer = CType(WeightedAverage(CType(resultRow(1), Double), CType(resultRow(2), Double), CType(resultRow(3), Double), CType(resultRow(4), Double)), Integer)

                If overallRisk < 10 Then
                    resultRow(5) = "Normal"
                ElseIf overallRisk < 15 Then
                    resultRow(5) = "Moderate"
                ElseIf overallRisk < 20 Then
                    resultRow(5) = "High"
                Else
                    resultRow(5) = "Very High"
                End If

                'resultRow(5) = CType(WeightedAverage(CType(resultRow(1), Double), CType(resultRow(2), Double), CType(resultRow(3), Double), CType(resultRow(4), Double)), Integer)

                resultTable.Rows.Add(resultRow)

            End If

        Next

        DataGridView1.DataSource = resultTable

    End Sub

    Private Function WeightedAverage(ByVal normal As Double, ByVal moderate As Double, ByVal high As Double, ByVal veryHigh As Double) As Double

        'the weight constants
        Dim normalWeight As Double = 1
        Dim moderateWeight As Double = 3
        Dim highWeight As Double = 6
        Dim veryHighWeight As Double = 8

        Dim result As Double = (normal * normalWeight + moderate * moderateWeight + high * highWeight + veryHigh * veryHighWeight) / 18.0
        Return result
    End Function

End Class